import wx
import ogre.renderer.OGRE as ogre 
#import OgreWindowPlugin


try:
	from agw import aui
except ImportError: # if it's not there locally, try the wxPython lib.
	import wx.lib.agw.aui as aui
class Do:
	def __init__(self,s,ns,selector,edit):
		self.s = s
		self.ns = ns
		
		self.selector = selector
		
		edit.addDo(self)
		pass
	
	def undo(self):
		self.selector.setCurrSelectNode(self.s,False)
		pass
	def redo(self):
		self.selector.setCurrSelectNode(self.ns,False)
		pass


class SceneSelector:

	def __init__(self,frame):
		self.frame = frame
		self.CameraPlugin = frame.plugins.CameraPlugin
		self.win = frame.plugins.OgreWindowPlugin
		self.frame = frame
		
		
		import environment
		self.rayQuery = self.win.sceneManager.createRayQuery(ogre.Ray(),0xFFFFFFFF & ~environment.QueryFlags)
		self.rayQuery.setSortByDistance(True,8)
		self.win.addMouseListener(self._MouseEventListener)

		self.curr_select_node = None
		self.curr_select_node_id = 0

		self.curr_select = None
		pass

	def getCurrSelect(self):
		return self.curr_select

	def getCurrSelectNode(self):
		return self.curr_select_node

	def setCurrSelect(self,new_select):
		self.curr_select = new_select
		pass
	
	def setCurrSelectNode(self,new_select,ignored_Do = True):
		if new_select == None :
			if self.curr_select_node:
				if ignored_Do :
					Do(self.curr_select_node,None,self,self.frame.plugins.EditPlugin)
				
				self.curr_select_node.showBoundingBox(False)
				self.curr_select_node = None
			return False

		if self.curr_select_node == None :
			if ignored_Do :
				Do(None,new_select,self,self.frame.plugins.EditPlugin)
			
			new_select.showBoundingBox(True)
			self.curr_select_node = new_select
			return True

		if new_select.getName() != self.curr_select_node.getName() :
			if ignored_Do :
				Do(self.curr_select_node,new_select,self,self.frame.plugins.EditPlugin)
			
			new_select.showBoundingBox(True)
			self.curr_select_node.showBoundingBox(False)
			self.curr_select_node = new_select
			return True
		return False

	def _MouseEventListener(self, event):
		if event.LeftDown():
			x,y = event.GetPosition()
			w,h = self.win.GetSizeTuple()
			_x = float(x)
			_y = float(y)
			_w = float(w)
			_h = float(h)
			ray = self.CameraPlugin.getCamera().getCameraToViewportRay(_x/_w, _y/_h)
			self.rayQuery.setRay(ray)
			res = self.rayQuery.execute()

			if len(res) > 0 :
				if  self.curr_select_node_id >= len(res) :
					self.curr_select_node_id = 0

				new_select = res[self.curr_select_node_id].movable.getParentSceneNode()

				if self.setCurrSelectNode(new_select):
					self.curr_select_node_id += 1

			else :
				self.setCurrSelectNode(None)
				self.curr_select_node_id = 0

			event.Skip() 
		#elif event.Moving():
		#	self.curr_select_node_id = 0

	pass

	def IHelp(self):
		return """key delete to delete the select"""

	def IBeginLoadScene(self):
		self.curr_select_node = None
		self.curr_select_node_id = 0

		self.curr_select = None
		pass

	def IClearScene(self):
		self.IBeginLoadScene()
		pass
	
##
def IInit(frame):
	return SceneSelector(frame)


	
	
	
	
	
	
	
	